package com.china08.yxyapi.service.sws.teaching.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.service.sws.teaching.ExamStatisticsService;
import com.china08.yxyapi.util.DateUtils;
import com.google.gson.Gson;

@Service
public class ExamStatisticsServiceImpl implements ExamStatisticsService {

    private static final Logger LOGGER = Logger.getLogger(ExamStatisticsServiceImpl.class);

    @Value(value = "${yx.rest.api.yx.path}")
    private String yxyApiYxUrl;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RestTemplate restTemplate;

    // examImport——class统计
    @Override
    public void getExamImportScoreForClass(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("createdDate").gte(startDate).lte(endDate);
        criteria.andOperator(new Criteria().orOperator(Criteria.where("status").is(1), Criteria.where("status").is(2)));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("classId")//
                        .count().as("count")//
                        .first("classId").as("classId")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<ClassExamModel> result = mongoTemplate.aggregate(aggregation, ClassExamModel.class);
        List<ClassExamModel> classExamModelLst = result.getMappedResults();
        List<ClassExamModel> sendDataLst = new ArrayList<ClassExamModel>();

        Integer index = 0;
        for (ClassExamModel classExamModel : classExamModelLst) {
            sendDataLst.add(classExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"classExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"ImportScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamImportScoreForClass方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamImportScoreForClass方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<ClassExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"classExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"ImportScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/score", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamImportScoreForClass方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamImportScoreForClass方法执行错误：" + e.getMessage());
            }
        }
    }

    // SendScore——class统计
    @Override
    public void getExamSendScoreForClass(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));

        Criteria criteria = Criteria.where("sendDate").is(DateUtils.formatDate4(dayDate));
        criteria.andOperator(Criteria.where("status").is(2));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("classId")//
                        .count().as("count")//
                        .first("classId").as("classId")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<ClassExamModel> result = mongoTemplate.aggregate(aggregation, ClassExamModel.class);
        List<ClassExamModel> classExamModelLst = result.getMappedResults();
        List<ClassExamModel> sendDataLst = new ArrayList<ClassExamModel>();

        Integer index = 0;
        for (ClassExamModel classExamModel : classExamModelLst) {
            sendDataLst.add(classExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"classExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"SendScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamSendScoreForClass方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamSendScoreForClass方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<ClassExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"classExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"SendScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/teachingclass/score", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamSendScoreForClass方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamSendScoreForClass方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class ClassExamModel {
        public Long count;
        public String classId;
        public String schoolId;
    }

    // examImport——School统计
    @Override
    public void getExamImportScoreForSchool(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("createdDate").gte(startDate).lte(endDate);
        criteria.andOperator(new Criteria().orOperator(Criteria.where("status").is(1), Criteria.where("status").is(2)));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("schoolId")//
                        .count().as("count")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SchoolExamModel> result = mongoTemplate.aggregate(aggregation, SchoolExamModel.class);
        List<SchoolExamModel> schoolExamModelLst = result.getMappedResults();
        List<SchoolExamModel> sendDataLst = new ArrayList<SchoolExamModel>();

        Integer index = 0;
        for (SchoolExamModel schoolExamModel : schoolExamModelLst) {
            sendDataLst.add(schoolExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"schoolExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"ImportScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/bureau/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamImportScoreForSchool方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamImportScoreForSchool方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SchoolExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"schoolExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"ImportScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/bureau/score",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamImportScoreForSchool方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamImportScoreForSchool方法执行错误：" + e.getMessage());
            }
        }
    }

    // SendScore——School统计
    @Override
    public void getExamSendScoreForSchool(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));

        Criteria criteria = Criteria.where("sendDate").is(DateUtils.formatDate4(dayDate));
        criteria.andOperator(Criteria.where("status").is(2));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("schoolId")//
                        .count().as("count")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SchoolExamModel> result = mongoTemplate.aggregate(aggregation, SchoolExamModel.class);
        List<SchoolExamModel> schoolExamModelLst = result.getMappedResults();
        List<SchoolExamModel> sendDataLst = new ArrayList<SchoolExamModel>();

        Integer index = 0;
        for (SchoolExamModel schoolExamModel : schoolExamModelLst) {
            sendDataLst.add(schoolExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"schoolExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"SendScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/bureau/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamSendScoreForSchool方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamSendScoreForSchool方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SchoolExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"schoolExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"SendScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/bureau/score",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamSendScoreForSchool方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamSendScoreForSchool方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SchoolExamModel {
        public Long count;
        public String schoolId;
    }

    // ImportScore——Subject统计
    @Override
    public void getExamImportScoreForSubjectAndStage(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("createdDate").gte(startDate).lte(endDate);
        criteria.andOperator(new Criteria().orOperator(Criteria.where("status").is(1), Criteria.where("status").is(2)));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("stage", "subject", "schoolId")//
                        .count().as("count")//
                        .first("stage").as("stage")//
                        .first("subject").as("subjectId")//
                        .first("subjectName").as("subjectName")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SubjectAndStageExamModel> result = mongoTemplate.aggregate(aggregation,
                SubjectAndStageExamModel.class);
        List<SubjectAndStageExamModel> subjectAndStageExamModelLst = result.getMappedResults();
        List<SubjectAndStageExamModel> sendDataLst = new ArrayList<SubjectAndStageExamModel>();

        Integer index = 0;
        for (SubjectAndStageExamModel subjectAndStageExamModel : subjectAndStageExamModelLst) {
            sendDataLst.add(subjectAndStageExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"subjectAndStageExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"ImportScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/score", formEntity,
                            String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamImportScoreForSubjectAndStage方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamImportScoreForSubjectAndStage方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SubjectAndStageExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"subjectAndStageExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"ImportScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/score", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamImportScoreForSubjectAndStage方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamImportScoreForSubjectAndStage方法执行错误：" + e.getMessage());
            }
        }
    }

    // SendScore——Subject统计
    @Override
    public void getExamSendScoreForSubjectAndStage(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));

        Criteria criteria = Criteria.where("sendDate").is(DateUtils.formatDate4(dayDate));
        criteria.andOperator(Criteria.where("status").is(2));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("stage", "subject", "schoolId")//
                        .count().as("count")//
                        .first("stage").as("stage")//
                        .first("subject").as("subjectId")//
                        .first("subjectName").as("subjectName")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<SubjectAndStageExamModel> result = mongoTemplate.aggregate(aggregation,
                SubjectAndStageExamModel.class);
        List<SubjectAndStageExamModel> subjectAndStageExamModelLst = result.getMappedResults();
        List<SubjectAndStageExamModel> sendDataLst = new ArrayList<SubjectAndStageExamModel>();

        Integer index = 0;
        for (SubjectAndStageExamModel subjectAndStageExamModel : subjectAndStageExamModelLst) {
            sendDataLst.add(subjectAndStageExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"subjectAndStageExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"SendScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/score", formEntity,
                            String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamSendScoreForSubjectAndStage方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamSendScoreForSubjectAndStage方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<SubjectAndStageExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"subjectAndStageExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"SendScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(
                        yxyApiYxUrl + "/comm/school/sws/teaching/schoolstagesubject/score", formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamSendScoreForSubjectAndStage方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamSendScoreForSubjectAndStage方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class SubjectAndStageExamModel {
        public Long count;
        public String schoolId;
        public String stage;
        public String subjectId;
        public String subjectName;
    }

    // ImportScore——User统计
    @Override
    public void getExamImportScoreForUser(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));
        Date startDate = DateUtils.getFirstDay(dayDate);
        Date endDate = DateUtils.getTimesnight(dayDate);

        Criteria criteria = Criteria.where("createdDate").gte(startDate).lte(endDate);
        criteria.andOperator(new Criteria().orOperator(Criteria.where("status").is(1), Criteria.where("status").is(2)));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("teacherId", "schoolId")//
                        .count().as("count")//
                        .first("teacherId").as("teacherId")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<UserExamModel> result = mongoTemplate.aggregate(aggregation, UserExamModel.class);
        List<UserExamModel> userExamModelLst = result.getMappedResults();
        List<UserExamModel> sendDataLst = new ArrayList<UserExamModel>();

        Integer index = 0;
        for (UserExamModel userExamModel : userExamModelLst) {
            sendDataLst.add(userExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"userExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"ImportScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teacher/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamImportScoreForUser方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamImportScoreForUser方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<UserExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"userExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"ImportScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/teacher/score",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamImportScoreForUser方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamImportScoreForUser方法执行错误：" + e.getMessage());
            }
        }
    }

    // SendScore——User统计
    @Override
    public void getExamSendScoreForUser(Integer day) throws ParseException {

        Date dayDate = DateUtils.getBeforeDay(DateUtils.formatStrDate4(day));

        Criteria criteria = Criteria.where("sendDate").is(DateUtils.formatDate4(dayDate));
        criteria.andOperator(Criteria.where("status").is(2));

        // 获取考试信息
        TypedAggregation<Exam> aggregation = Aggregation.newAggregation(Exam.class, Aggregation.match(criteria),
                Aggregation.group("teacherId", "schoolId")//
                        .count().as("count")//
                        .first("teacherId").as("teacherId")//
                        .first("schoolId").as("schoolId"));

        AggregationResults<UserExamModel> result = mongoTemplate.aggregate(aggregation, UserExamModel.class);
        List<UserExamModel> userExamModelLst = result.getMappedResults();
        List<UserExamModel> sendDataLst = new ArrayList<UserExamModel>();

        Integer index = 0;
        for (UserExamModel userExamModel : userExamModelLst) {
            sendDataLst.add(userExamModel);
            index++;
            if (index == 100) {
                try {
                    HttpHeaders headers = new HttpHeaders();
                    MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                    headers.setContentType(type);
                    headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                    Gson gson = new Gson();
                    StringBuffer objectBuffer = new StringBuffer();
                    objectBuffer.append("{");
                    objectBuffer.append("\"userExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                    objectBuffer.append("\"type\":\"SendScore\",");
                    objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                    objectBuffer.append("}");
                    HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                    String httpResult = restTemplate.postForObject(
                            yxyApiYxUrl + "/comm/school/sws/teaching/teacher/score", formEntity, String.class);
                    if (httpResult.contains("success")) {
                        System.out.println("getExamSendScoreForUser方法执行ok");
                    }
                } catch (Exception e) {
                    LOGGER.info("getExamSendScoreForUser方法执行错误：" + e.getMessage());
                }
                sendDataLst = new ArrayList<UserExamModel>();
                index = 0;
            }
        }
        if (sendDataLst.size() > 0) {
            try {
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("Accept", MediaType.APPLICATION_JSON_UTF8_VALUE.toString());
                Gson gson = new Gson();
                StringBuffer objectBuffer = new StringBuffer();
                objectBuffer.append("{");
                objectBuffer.append("\"userExamModelLst\":" + gson.toJson(sendDataLst) + ",");
                objectBuffer.append("\"type\":\"SendScore\",");
                objectBuffer.append("\"dateTime\":" + DateUtils.formatDate4(dayDate));
                objectBuffer.append("}");
                HttpEntity<String> formEntity = new HttpEntity<String>(objectBuffer.toString(), headers);
                String httpResult = restTemplate.postForObject(yxyApiYxUrl + "/comm/school/sws/teaching/teacher/score",
                        formEntity, String.class);
                if (httpResult.contains("success")) {
                    System.out.println("getExamSendScoreForUser方法执行ok");
                }
            } catch (Exception e) {
                LOGGER.info("getExamSendScoreForUser方法执行错误：" + e.getMessage());
            }
        }
    }

    protected static class UserExamModel {
        public Long count;
        public String schoolId;
        public String teacherId;
    }
}
